package cn.stronglink.assetmanage.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.inventory.model.InventoryDetailModel;
import cn.stronglink.assetmanage.entity.Department;
import cn.stronglink.assetmanage.entity.Node;
import cn.stronglink.assetmanage.utils.ToastUtil;
import cn.stronglink.assetmanage.widget.treeview.SimpleTreeAdapter;
import cn.stronglink.assetmanage.widget.treeview.TreeListViewAdapter;

/**
 * Created by chencongcong on 2018/6/19.
 */

public class MenuOrderDialog extends Dialog {

    //部门调用示例
//    InventoryDetailModel inventoryDetailModel = new InventoryDetailModel();
//    MenuOrderDialog.Builder builder = new MenuOrderDialog.Builder(getContext());
//    final List<Department> departmentList = inventoryDetailModel.getDepartment();
//                builder.setDatas(departmentList);
//                builder.setTitle("使用部门");
//    MenuOrderDialog menuOrderDialog =  builder.create();
//                menuOrderDialog.setItemClickListener(new MenuOrderDialog.onItemClickListener() {
//        @Override
//        public void onItemClick(Node node, int position) {
//            //点击了
//            ToastUtil.toastShort(node.getName());
//        }
//    });
//                menuOrderDialog.show();
    public interface onItemClickListener {
        void onItemClick(Node node, int position);
    }

    private static onItemClickListener itemClickListener;

    public onItemClickListener getItemClickListener() {
        return itemClickListener;
    }

    public void setItemClickListener(onItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    public MenuOrderDialog(Context context) {
        super(context);
    }

    public MenuOrderDialog(Context context, int theme) {
        super(context, theme);
    }


    public static class Builder {
        private Context context;
        private String title;
        private String negativeButtonText;
        private String positiveButtonText;
        private List<Department> datas;
        private OnClickListener positiveOnClickListener;
        private OnClickListener negativeOnClickListener;
        private SimpleTreeAdapter treeListViewAdapter = null;

        public Builder(Context context) {
            this.context = context;
        }


        public Builder setDatas(List<Department> datas) {
            this.datas = datas;
            return this;
        }


        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeOnClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeOnClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(int posiviteButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(posiviteButtonText);
            this.positiveOnClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String posiviteButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = posiviteButtonText;
            this.positiveOnClickListener = listener;
            return this;
        }


        public MenuOrderDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final MenuOrderDialog dialog = new MenuOrderDialog(context, R.style.loading_dialog);
            View layout = inflater.inflate(R.layout.dialog_lv_tree, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.title)).setText(title);
            // set the cancel button
            if (negativeButtonText != null) {
                if (negativeOnClickListener != null) {
                    ((Button) layout.findViewById(R.id.btn_cancel))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    negativeOnClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.btn_cancel).setVisibility(
                        View.GONE);
            }

            if (positiveButtonText != null) {
                if (positiveOnClickListener != null) {
                    ((Button) layout.findViewById(R.id.btn_ok))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveOnClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.btn_ok).setVisibility(
                        View.GONE);
            }
            ListView listView = (ListView) layout.findViewById(R.id.lv_tree);
            try {
                treeListViewAdapter = new SimpleTreeAdapter<Department>(listView, context, datas, 10);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            listView.setAdapter(treeListViewAdapter);
            treeListViewAdapter.setOnTreeNodeClickListener(new TreeListViewAdapter.OnTreeNodeClickListener() {
                @Override
                public void onClick(Node node, int position) {
                    treeListViewAdapter.setSelectPosition(position);
                    treeListViewAdapter.notifyDataSetChanged();
                    if (itemClickListener != null) {
                        itemClickListener.onItemClick(node, position);
                    }
                }
            });


            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(true);// 设置点击屏幕Dialog不消失
            return dialog;
        }

    }
}
